# -*- coding: utf-8 -*-
# created on 2016/5/14

from mathsolver.functions.base import *
from sympy import sqrt, latex, Pow, sympify


# 求平方根
class ShuYuShiErCiGenShi004(BaseFunction):
    """
    ShuYuShiErCiGenShi004().solver(BasePoly("1/4"))
    """

    def solver(self, *args):
        poly = args[0].sympify()
        value = sqrt(poly).simplify()
        self.steps.append(["由平方根的定义得，", "%s的平方根是%s,%s" % (new_latex(poly), new_latex(value), new_latex(-value))])
        self.label.add("求平方根")
        self.output.append(BasePoly(value))
        self.output.append(BasePoly(-value))
        return self


# 求算术平方根
class ShuYuShiErCiGenShi005(BaseFunction):
    """
    ShuYuShiErCiGenShi005().solver(BasePoly("1/4"))
    """

    def solver(self, *args):
        poly = args[0].sympify()
        value = sqrt(poly).simplify()
        self.steps.append(["由算术平方根的定义得，", "%s的算术平方根是%s" % (new_latex(poly), new_latex(value))])
        self.label.add("求算术平方根")
        self.output.append(BasePoly(value))
        return self


# 求立方根
class ShuYuShiErCiGenShi006(BaseFunction):
    def solver(self, *args):
        poly = args[0].sympify()
        if len(poly.free_symbols) == 0 and poly < 0:
            value = -Pow(-poly, sympify("1/3"))
        else:
            value = Pow(poly, sympify("1/3"))

        self.steps.append(["由立方根的定义得，", "%s的立方根是%s" % (new_latex(poly), new_latex(value))])
        self.label.add("求立方根")
        self.output.append(BasePoly(value))
        return self


if __name__ == '__main__':
    pass
